//
//  DBParserAstNode.m
//  parser
//
//  Created by Daniel Braun on 10/06/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "DBParserAstNode.h"
#import "DBLexer.h"

@implementation DBParserAstNode

+ (id) astNodeWithToken:(DBLexerTokenMatch *) t
{
	return [[[[self class]alloc]initWithToken:t]autorelease];
}

- (id) initWithToken:(DBLexerTokenMatch *) t
{
	self = [super init];
	if (self != nil) {
		//<#initializations#>
		tok=[t retain];
	}
	return self;
}
- (id) init {
	NSAssert(0, @"init should not be called, shall use initWithToken");
	self = [super init];
	if (self != nil) {
		//<#initializations#>
	}
	return self;
}

- (void) dealloc {
	[left release];
	[right release];
	[tok release];
	[super dealloc];
}


- (void) setLeaf:(BOOL)l
{
	leaf=l;
}
- (id) left
{
	return left;
}

- (id) right
{
	return right;
}

- (DBLexerTokenMatch *) tok
{
	return tok;
}
- (BOOL) isLeaf
{
	return leaf;
}

- (void) setLeft:(id)l
{
	NSAssert(!leaf, @"attempt to add child to leaf");
	NSAssert(!left, @"left already set");
	[left release];
	left=[l retain];
}

- (void) setRight:(id)r
{
	NSAssert(!leaf, @"attempt to add child to leaf");
	NSAssert(!right, @"right already set");
	[right release];
	right=[r retain];
}

- (void) setToken:(DBLexerTokenMatch *) t
{
	[tok release];
	tok=[t retain];
}


- (NSString *) description
{
	//if (leaf) return [NSString stringWithFormat:@"[%@]", tok];
	//return [NSString stringWithFormat:@"<%@>-[%@]-<%@>", left, tok, right];
	if (leaf) return [NSString stringWithFormat:@"%@", [tok matchString]];
	return [NSString stringWithFormat:@"(%@ %@ %@)", left, [tok matchString], right];

}

@end
